package uk.co.davedexter.flickrdatr;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.RequestContext;
import com.aetrion.flickr.auth.Auth;
import com.aetrion.flickr.auth.AuthInterface;
import com.aetrion.flickr.auth.Permission;
import com.aetrion.flickr.photos.Exif;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.photos.PhotosInterface;
import com.aetrion.flickr.photos.SearchParameters;
import com.aetrion.flickr.util.FileAuthStore;

public class DateChangeTest {

	private Flickr flickr;
	private FileAuthStore store;
	AuthInterface authInterface;
	Auth auth;
	private static String apiKey = "45bc65fa6dc96825653b96e5333d0072";
	private static String secret = "a0c74b31cbde681d";
	private static String nSID = "47213205@N04";

	DateChangeTest() {
		// Set up the flickr connection
		try {
			flickr = new Flickr(apiKey, secret, new REST());
			authInterface = flickr.getAuthInterface();

			RequestContext rc = RequestContext.getRequestContext();

			File authDir = new File("authstore");
			if (authDir.exists() & authDir.isDirectory()) {
				System.out.println("Loading existing authorisation.");
				store = new FileAuthStore(authDir);
				auth = store.retrieve(nSID);
				rc.setAuth(auth);
			} else {
				// Prompt the user.
				String frob = authInterface.getFrob();
				URL url = authInterface.buildAuthenticationUrl(Permission.WRITE, frob);
				System.out.println("Press return after you granted access at this URL:");
				System.out.println(url.toExternalForm());
				BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
				infile.readLine();
				auth = authInterface.getToken(frob);

				rc.setAuth(auth);

				store = new FileAuthStore(new File("authstore"));
				store.store(auth);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (FlickrException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	private void changeDate() {
		try {
			PhotosInterface photoInt = flickr.getPhotosInterface();
			SearchParameters searchParams = new SearchParameters();
			searchParams.setMedia("photos");
			searchParams.setUserId(nSID);
			searchParams.setText("IMG_9148 christmas2010");
			PhotoList results = photoInt.search(searchParams, 500, 0);
			int size = results.size();
			System.out.println("Size: " + size);
			Photo p = (Photo) results.get(0);
			System.out.println(p.getTitle());
			
			
			System.out.println(p.getTags());
			System.out.println(p.getDateAdded());
			System.out.println(p.getDatePosted());
			System.out.println(p.getDateTaken());

			Date dateTaken = new Date();
			Date datePosted = new Date();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				dateTaken = df.parse("2010-12-29 19:45:00");
				datePosted = df.parse("2010-12-29 19:45:00");
			} catch (ParseException e) {
				e.printStackTrace();
			}

			photoInt.setDates(p.getId(), datePosted, dateTaken, null);
			
			
			Collection allExif = photoInt.getExif(p.getId(), p.getSecret());
//			
//			Iterator itr = allExif.iterator();
//			while (itr.hasNext()) {
//				Exif exif = (Exif)itr.next();
////				if (exif.getTag().toLowerCase().contains("date")) {
////					System.out.println(exif.getTag() + "\t" + exif.getRaw());
////				}
//				System.out.println(exif.getTag() + "\t" + exif.getRaw());
//			}
//			
			Photo photo = photoInt.getInfo(p.getId(), p.getSecret());
			System.out.println(photo.getTakenGranularity());
			
			
			//
			// try {
			// String str_date = "2011-01-01 16:33:00";
			// DateFormat formatter;
			// Date date;
			// formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			// date = (Date) formatter.parse(str_date);
			// long longDate = date.getTime();
			// p.setDatePosted(longDate);
			// System.out.println(p.getDatePosted());
			// p.setTitle("IMG_9148a.jpg");
			//
			// } catch (ParseException e) {
			// System.out.println("Exception :" + e);
			// }

		} catch (FlickrException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		DateChangeTest test = new DateChangeTest();
		test.changeDate();
	}

}
